package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.dto.ProjectPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingPlanProject;
import com.zzyl.entity.NursingProject;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingProjectVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class NursingPlanServiceImpl implements NursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    /**
     * 查询所有护理计划
     *
     * @return
     */
    @Override
    public List<NursingPlanVo> getAllNursingPlan() {
        return nursingPlanMapper.getAllNursingPlan();
    }

    /**
     * 根据id查询护理计划
     *
     * @param id
     * @return
     */
    @Override
    public NursingPlanVo getNursingPlanById(Long id) {
        //1.根据id查询护理计划
        NursingPlanVo nursingPlan = nursingPlanMapper.getNursingPlanById(id);
        //2.根据根据计划id查询获取关联表信息及项目名称
        List<ProjectPlanDto> projectPlanDtos = nursingPlanMapper. getPlanProjects(id);

        //4.将项目信息集合设置到护理计划对象中
        nursingPlan.setProjectPlans(projectPlanDtos);
        return nursingPlan;
    }

    /**
     * 修改护理计划
     */
    @Override
    @Transactional
    public void updateNursingPlan(NursingPlanDto nursingPlanDto) {
        //1.修改护理计划
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        if (UserThreadLocal.getUserId() != null) {
            nursingPlan.setUpdateBy(UserThreadLocal.getUserId());
        } else {
            nursingPlan.setUpdateBy(1l);
        }
        nursingPlan.setUpdateTime(LocalDateTime.now());
        nursingPlanMapper.updatePlan(nursingPlan);
        //2.修改护理计划、护理项目关联表
        //①根据planId删除关联表
        nursingPlanMapper.deleteByPlanId(nursingPlan.getId());
        //②重新插入计划、项目关系表信息
        //封装计划、项目关系表集合
        List<NursingPlanProject> nursingPlanProjectList = new ArrayList<>();
        List<ProjectPlanDto> projectPlans = nursingPlanDto.getProjectPlans();
        for (ProjectPlanDto projectPlanDto : projectPlans) {
            NursingPlanProject nursingPlanProject = new NursingPlanProject();
            Integer count = projectPlanDto.getCount();
            Long projectId = projectPlanDto.getProjectId();
            nursingPlanProject.setExecuteTime(projectPlanDto.getExecuteTime());
            nursingPlanProject.setExecuteFrequency(projectPlanDto.getExecuteFrequency());
            nursingPlanProject.setExecuteCycle(projectPlanDto.getExecuteCycle());
            nursingPlanProject.setCount(count);
            nursingPlanProject.setPlanId(nursingPlan.getId());
            nursingPlanProject.setProjectId(projectId);
            nursingPlanProjectList.add(nursingPlanProject);
        }
        //批量插入计划、项目关系表信息
        System.out.println(nursingPlanProjectList);
        nursingPlanMapper.insertNursingPlanProject(nursingPlanProjectList);
    }

    /**
     * 新增护理计划
     *
     * @param nursingPlanDto
     */
    @Override
    @Transactional
    public void addNursingPlan(NursingPlanDto nursingPlanDto) {
        //1.新增护理计划
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        if (UserThreadLocal.getUserId() != null) {
            nursingPlan.setCreateBy(UserThreadLocal.getUserId());
        } else {
            nursingPlan.setCreateBy(1l);
        }
        nursingPlan.setCreateTime(LocalDateTime.now());
        //设置状态值，默认为启用 1
        nursingPlan.setStatus(1);
        nursingPlanMapper.addNursingPlan(nursingPlan);//该方法会将id返回nursingPlan
        Long planId = nursingPlan.getId();
        //2.插入计划、项目关系表集合
        //封装计划、项目关系表集合
        List<NursingPlanProject> nursingPlanProjectList = new ArrayList<>();
        List<ProjectPlanDto> ProjectPlans = nursingPlanDto.getProjectPlans();
        for (ProjectPlanDto projectPlanDto : ProjectPlans) {
            NursingPlanProject nursingPlanProject = new NursingPlanProject();
            Integer count = projectPlanDto.getCount();
            Long projectId = projectPlanDto.getProjectId();
            nursingPlanProject.setExecuteTime(projectPlanDto.getExecuteTime());
            nursingPlanProject.setExecuteFrequency(projectPlanDto.getExecuteFrequency());
            nursingPlanProject.setExecuteCycle(projectPlanDto.getExecuteCycle());
            nursingPlanProject.setCount(count);
            nursingPlanProject.setPlanId(planId);
            nursingPlanProject.setProjectId(projectId);
            nursingPlanProjectList.add(nursingPlanProject);
        }
        //批量插入计划、项目关系表信息
        System.out.println(nursingPlanProjectList);
        nursingPlanMapper.insertNursingPlanProject(nursingPlanProjectList);
    }

    /**
     * 分页条件查询
     *
     * @param name
     * @param status
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageResponse getPlanByPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingPlanVo> page = nursingPlanMapper.getNursingPlanByPage(name, status);
        return PageResponse.of(page, NursingPlanVo.class);
    }

    /**
     * 根据id删除护理计划
     *
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        //1.删除护理计划
        nursingPlanMapper.deleteById(id);
        //2.删除护理计划与项目关联表的的数据
        nursingPlanMapper.deleteByPlanId(id);
    }

    /**
     * 根据id启用/禁用护理计划
     *
     * @param id
     * @param status
     */
    @Override
    public void updateStatusById(Long id, Integer status) {
        Long updateBy;
        if (UserThreadLocal.getUserId() != null) {
            updateBy = UserThreadLocal.getUserId();
        } else {
            updateBy= 1l;
        }
        LocalDateTime updateTime = LocalDateTime.now();
        nursingPlanMapper.updateStatusById(id, status,updateBy,updateTime);
    }
}
